package com.ruoyi.business.controller;

import com.alibaba.fastjson.JSON;
import com.ruoyi.business.domain.CarPackageAudit;
import com.ruoyi.business.domain.ServiceItem;
import com.ruoyi.business.service.ICarPackageAuditService;
import com.ruoyi.business.service.IProcessService;
import com.ruoyi.business.vo.AuditInfo;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import org.apache.commons.io.IOUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.util.Collections;
import java.util.List;

/**
 * 审核列表Controller
 * 
 * @author gekor
 * @date 2021-07-24
 */
@Controller
@RequestMapping("/business/carPackageAudit")
public class CarPackageAuditController extends BaseController
{
    private String prefix = "business/carPackageAudit";

    @Autowired
    private ICarPackageAuditService carPackageAuditService;
    @Autowired
    private IProcessService processService;

    @RequiresPermissions("business:carPackageAudit:view")
    @GetMapping()
    public String carPackageAudit()
    {
        return prefix + "/carPackageAudit";
    }

    /**
     * 查询审核列表列表
     */
    @RequiresPermissions("business:carPackageAudit:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(CarPackageAudit carPackageAudit)
    {
        startPage();
        List<CarPackageAudit> list = carPackageAuditService.selectCarPackageAuditList(carPackageAudit);
        return getDataTable(list);
    }

    /**
     * 返回历史审批记录页面
     * @param instanceId
     * @return
     */
    @RequestMapping("/historicAudit/{instanceId}")
    public String historicAudit(@PathVariable String instanceId,ModelMap modelMap){

        modelMap.put("instanceId",instanceId);
        return prefix + "/auditInfoList";
    }

    /**
     * 返回历史审批记录数据
     * @param instanceId
     * @return
     */
    @RequestMapping("/listHistory/{instanceId}")
    @ResponseBody
    public TableDataInfo listHistory(@PathVariable String instanceId){
        List<AuditInfo> list = carPackageAuditService.selectHistoricAudit(instanceId);
        return getDataTable(list);
    }

    /**
     * 查看流程图进度
     */
    @RequestMapping("/checkProgress/{instanceId}/{taskId}")
    @ResponseBody
    public void checkProgress(@PathVariable String instanceId, @PathVariable String taskId, HttpServletResponse response) throws IOException {
        String definitionId = processService.selectProcessDefinitonIdByInstanceId(instanceId);
        InputStream inputStream = null;
        try{
            Long.valueOf(taskId);   // 转换出现异常说明没有正在运行的任务，任务审核通过或已撤销
            String taskDefKey = processService.selectTaskDefinitionKeyByTaskId(taskId);
            inputStream = processService.getImgInputStream(definitionId, Collections.singletonList(taskDefKey),Collections.EMPTY_LIST);
        } catch (Exception e){
            inputStream = processService.getImgInputStream(definitionId, Collections.EMPTY_LIST,Collections.EMPTY_LIST);
        }
        IOUtils.copy(inputStream, response.getOutputStream());
    }

    /**
     * 撤销流程审核实例
     */
    @RequestMapping("/revoke/{instanceId}/{packageId}")
    @ResponseBody
    public AjaxResult revoke(@PathVariable String instanceId, @PathVariable Long packageId){
        return toAjax(carPackageAuditService.revokeAudit(instanceId,packageId));
    }

    /**
     * 我的待办页面
     */
    @RequestMapping("/todoPage")
    public String todoPage(){
        return prefix + "/todoPage";
    }

    /**
     * 我的待办数据
     */
    @RequestMapping("/todoPageList")
    @ResponseBody
    public TableDataInfo todoPageList(){
        startPage();
        List<CarPackageAudit> list = carPackageAuditService.selectTodoCarPackageAuditList();
        return getDataTable(list);
    }

    /**
     * 返回审批页面
     */
    @RequestMapping("/audit/{taskId}")
    public String audit(@PathVariable String taskId, Long itemId, Model model){
        model.addAttribute("taskId",taskId);
        model.addAttribute("itemId",itemId);
        return prefix + "/auditPage";
    }

    /**
     * 完成审批
     */
    @RequestMapping("/doAudit")
    @ResponseBody
    public AjaxResult doAudit(String taskId, boolean auditStatus, String comment, Long itemId){
        return toAjax(carPackageAuditService.doAudit(taskId,auditStatus,comment,itemId));
    }

    /**
     * 跳转修改表单页面
     */
    @RequestMapping("/reEdit/{auditId}")
    public String reEdit(@PathVariable Long auditId, ModelMap modelMap){
        CarPackageAudit audit = carPackageAuditService.selectCarPackageAuditById(auditId);
        ServiceItem serviceItem = JSON.parseObject(audit.getServiceItemInfo(), ServiceItem.class);
        modelMap.put("serviceItem",serviceItem);
        modelMap.put("auditId",audit.getId());
        return prefix + "/reEditPage";
    }

    /**
     * 保存修改后的表单数据
     */
    @RequestMapping("/reEditSave")
    @ResponseBody
    public AjaxResult reEditSave(ServiceItem serviceItem, Long auditId){
        return toAjax(carPackageAuditService.reEditSave(serviceItem,auditId));
    }

    /**
     * 重新申请审核
     */
    @RequestMapping("/reApply/{taskId}")
    @ResponseBody
    public AjaxResult reApply(@PathVariable String taskId, String instanceId){
        return toAjax(carPackageAuditService.reApply(taskId,instanceId));
    }

    /**
     * 返回我的已办页面
     */
    @RequestMapping("/donePage")
    public String doPage(){
        return prefix + "/donePage";
    }

    /**
     * 返回我的已办页面数据
     */
    @RequestMapping("/donePageList")
    @ResponseBody
    public TableDataInfo donePageList(){
        startPage();
        List<CarPackageAudit> list = carPackageAuditService.selectDoneCarPackageAuditList();
        return getDataTable(list);
    }
}
